package fr.neyb.bernouilli.common.exception;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

final class MessageFormatter {
	private MessageFormatter() {
	}

	private final static Pattern argsDetector = Pattern.compile("\\{(\\d+)\\}");

	public static String messageOf(String msg, Object... args) {
		String result = msg;
		Matcher m = argsDetector.matcher(msg);
		int index;

		while (m.find()) {
			index = Integer.parseInt(m.group(1));
			if (args.length >= index)
				result = result.replace(m.group(), args[index].toString());
		}
		return result;
	}
}
